﻿/**
* ...
* @author alijayameilio
*/

package particling.force;

class RadialOuterForce extends Force
{

	public function new(__x:Float, __y:Float, __power:Float) 
	{
		super(__x, __y, __power);
	}
	
	public override function getXForce(__xPos:Float, __yPos:Float):Float
	{
		var xForce:Float = 0;
		var direction = calculateDirection(__xPos, __yPos);
		if (direction < 180)
		{
			xForce = 1 * Math.cos(Math.abs(direction - 90) * Math.PI / 180) * power;
		} else
		{
			xForce = -1 * Math.cos(Math.abs(270 - direction) * Math.PI / 180) * power;
		}
		return xForce;
	}
	public override function getYForce(__xPos:Float, __yPos:Float):Float
	{
		var yForce:Float = 0;
		var direction = calculateDirection(__xPos, __yPos);
		if (direction < 180)
		{
			if (direction < 90)
			{
				yForce = -1 * Math.sin(Math.abs(direction - 90) * Math.PI / 180) * power;
			} else
			{
				yForce = 1 * Math.sin(Math.abs(direction - 90) * Math.PI / 180) * power;
			}
		} else
		{
			if (direction < 270)
			{
				yForce = 1 * Math.sin(Math.abs(270 - direction) * Math.PI / 180) * power;
			} else
			{
				yForce = -1 * Math.sin(Math.abs(270 - direction) * Math.PI / 180) * power;
			}
		}
		return yForce;
	}
	
	private function calculateDirection(__xPos:Float, __yPos:Float):Float
	{
		var deltaX = __xPos - x;
		var deltaY = __yPos - y;
		var direction = Math.atan2(Math.abs(deltaY), Math.abs(deltaX)) * 180 / Math.PI;
		if (deltaX < 0)
		{
			if (deltaY < 0)
			{
				direction = 270 + direction; 
			} else
			{
				direction = 270 - direction;
			}
		} else
		{
			if (deltaY < 0)
			{
				direction = 90 - direction; 
			} else
			{
				direction = 90 + direction;
			}
		}
		return direction;
	}
}